$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var work = JSON.parse(localStorage.getItem("work"));
    var pass = 1;
    var focus = true;

    $(".head .icon-lishi1").on("click", function () {
        localStorage.setItem("acidhtml", "acid_one");
        window.location.href = "acid_lishi.html";
    });

    //搜索框 获取焦点
    $("#goods_id").focus();
    /*$("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });*/

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        localStorage.removeItem("work");
        window.location.href = "acid.html";
    });

    //根据 id 得到 工单信息、机台信息
    getWorkFun(work.id);

    function getWorkFun(id) {
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/acid/getAcidtaskplandetailById',
            data: {
                id: id,
            },
            dataType: 'json',
            json: 'callback',
            success: function (res) {
                loading_hide();
                if (res.success) {
                    var data = res.result;
                    // work = data;
                    $("section span.workplanNumber").html(data.plan.plan.workplanNumber);
                    $("section span.kh").html(data.plan.plan.customerName);
                    $("section span.ddlx").html(data.plan.plan.saleordertype);
                    $("section span.work_gh").html(data.plan.inventorySteel);
                    $("section span.work_gg").html(data.plan.inventorySpecification);
                    $("section span.work_jhl").html(data.plan.root);
                    $("section span.work_wcl").html(data.plan.completeRoot || "0");
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                loading_hide();
                console.log(error);
                $alert(error.responseText);
            }
        });
    }

    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if ($(".content .material .card-content .onematerial").length == 2) {
                $alert("存在两件盘条信息，请先收线");
                pass = 1;
                return;
            }
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描盘条标签！");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            } else {
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/acid/getAcidmaterial',
                    data: {
                        id: work.id,
                        serial: val
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            var data = res.result;
                            for (var i = 0; i < $(".content .material .card-content .onematerial").length; i++) {
                                var number = $(".content .material .card-content .onematerial").find("span.number").text();
                                if (data.goods_no == number) {
                                    return $alert("不可重复扫描！");
                                }
                            }
                            $alert(data.goods_no + "扫描成功");
                            //获得扫描的盘条
                            var html = '<div class="card-content-inner onematerial">' +
                                '<a href="#"><span class="icon iconfont icon-cuohao shanchu pull-right"></span></a>' +
                                '<p><span>编号 ：</span><span class="number">' + data.goods_no + '</span></p>' +
                                '<p><span>炉号 ：</span><span class="lh">' + data.batch_code + '</span>&nbsp;&nbsp;<span>卷号 ：</span><span class="column">' + data.hook_no + '</span></p>' +
                                '<p><span>重量 ：</span><span class="zl">' + (data.goods_device_weight || data.goods_weight) + '</span> T</p>' +
                                '</div>'
                            $(".content .material .card-content").append(html);
                        } else {
                            console.log(res);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        pass = 1;
                        loading_hide();
                        console.log(error);
                        $alert(error.responseText);
                    }
                });
            }
        }
    });

    //点击 删除
    $(".content .material .card-content").on("click", ".shanchu", function () {
        var jianhao = $(this);
        $.confirm('确定删除此盘条吗？', function () {
            jianhao.parent().parent().remove();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    //点击 获取编号
    $(".foot .del_receive").on("click", function () {
        if ($(".content .material .card-content .onematerial").length == 2) {
            $alert("存在两件盘条信息，请先收线");
            pass = 1;
            return;
        } else {
            if (pass == 0) {
                return
            }
            // pass = 0;
            // pass = 1;
            focus = false;
            $(".mengceng").show();
            $("div.div_yuanliao").show();
            $("div.div_yuanliao input").val("");
            // $("div.div_yuanliao input.cpbh").val(res.result.number);
            $("div.div_yuanliao input.cplh").focus();
            /*$.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/acid/getAcidNumber',
                data: {
                    id: work.id
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        focus = false;
                        $(".mengceng").show();
                        $("div.div_yuanliao").show();
                        $("div.div_yuanliao input").val("");
                        $("div.div_yuanliao input.cpbh").val(res.result.number);
                        $("div.div_yuanliao input.cplh").focus();
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    console.log(error);
                    $alert(error.responseText);
                }
            });*/
        }
    });
    //点击 原料 取消
    $(".yl_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_yuanliao").hide();
        $("#goods_id").val("").focus();
        focus = false;
    });
    //点击 原料 确认
    $(".yl_queding").click(function () {
        if (pass == 0){
            return
        }
        pass = 0;
        if (!$("div.div_yuanliao input.cplh").val()) {
            pass = 1;
            $alert("请输入炉号");
            $("div.div_yuanliao input.cplh").focus();
        } else if (!$("div.div_yuanliao input.cpjh").val()) {
            pass = 1;
            $alert("请输入卷号");
            $("div.div_yuanliao input.cpjh").focus();
        } else if (!$("div.div_yuanliao input.cpzl").val()) {
            pass = 1;
            $alert("请输入重量");
            $("div.div_yuanliao input.cpzl").focus();
        } else {
            loading_show("核对中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/acid/checkNumber',
                data: {
                    stove: $("div.div_yuanliao input.cplh").val(),
                    column: $("div.div_yuanliao input.cpjh").val(),
                    weight: Number($("div.div_yuanliao input.cpzl").val())
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    pass = 1;
                    if (res.success) {
                        var data = res.result;
                        for (var i = 0; i < $(".content .material .card-content .onematerial").length; i++) {
                            var number = $(".content .material .card-content .onematerial").find("span.number").text();
                            if (data.number == number) {
                                return $alert("编号不可重复！");
                            }
                        }
                        //获得扫描的盘条
                        var html = '<div class="card-content-inner onematerial">' +
                            '<a href="#"><span class="icon iconfont icon-cuohao shanchu pull-right"></span></a>' +
                            '<p><span>编号 ：</span><span class="number">' + data.number + '</span></p>' +
                            '<p><span>炉号 ：</span><span class="lh">' + data.stove + '</span>&nbsp;&nbsp;' +
                            '<span>卷号 ：</span><span class="column">' + data.column + '</span></p>' +
                            '<p><span>重量 ：</span><span class="zl">' + data.weight + '</span></p>' +
                            '</div>'
                        $(".content .material .card-content").append(html);
                        $alert("盘条数据添加成功！");
                        $(".mengceng").hide();
                        $("div.div_yuanliao").hide();
                        $("#goods_id").val("").focus();
                        focus = true;
                    } else {
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    console.log(error);
                    loading_hide();
                    $alert(error.responseText);
                }
            });
        }
    });

    // 点击 收线
    $(".foot .add_receive").on("click", function (event) {
        if ($(".content .material .card-content .onematerial").length == 0) {
            $alert("请先扫描盘条标签");
            pass = 1;
        } else {
            // var weight = $(".content input.mz").val();
            // if (!weight) {
            //     $alert("请先获取重量！");
            //     pass = 1;
            // } else if (Number(weight) < 0) {
            //     $alert("重量不可小于0！");
            //     pass = 1;
            // } else {
                var data = [];
                for (var i = 0; i < $(".content .material .card-content .onematerial").length; i++) {
                    var batch = $(".content .material .card-content .onematerial").eq(i).find("span.number").text();
                    var stove = $(".content .material .card-content .onematerial").eq(i).find("span.lh").text();
                    var column = $(".content .material .card-content .onematerial").eq(i).find("span.column").text();
                    var zl = $(".content .material .card-content .onematerial").eq(i).find("span.zl").text();
                    data.push({batch: batch, stove: stove, column: column, weight: zl});
                }
                $.confirm("共" + $(".content .material .card-content .onematerial").length + "件，确认生产吗？", function () {
                    if (pass == 0) {
                        return
                    }
                    pass = 0;
                    loading_show("收线中...");
                    $.ajax({
                        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                        type: "post",
                        url: url + '/app/acid/acidTakeup',
                        data: {
                            id: work.id,
                            // weight: weight,
                            data: JSON.stringify(data)
                        },
                        dataType: 'json',
                        json: 'callback',
                        success: function (res) {
                            pass=1;
                            if (res.success) {
                                loading_hide();
                                if (res.complete) {
                                    $alert("收线成功，本计划已完成！");
                                    setTimeout(function () {
                                        window.location.href = "acid.html";
                                    }, 1200);
                                } else {
                                    $alert("收线成功");
                                    setTimeout(function () {
                                        window.location.href = "acid_one.html";
                                    }, 1200);
                                }
                                // 打印酸洗标签
                                if (res.result.length > 0) {
                                    for (var i = 0; i < res.result.length; i++) {
                                        takeupPrint("acidTakeup", res.result[i]);
                                    }
                                }
                            } else {
                                console.log(res);
                                loading_hide();
                                $alert(res.message);
                            }
                        },
                        error: function (error) {
                            pass=1;
                            console.log(error);
                            loading_hide();
                            $alert(error.responseText);
                        }
                    });
                });
            // }
        }
    });

});
